package com.matrix.admin.open.test.service.impl;

import com.matrix.admin.open.test.service.IHttpApiService;
import com.matrix.common.enums.SysDefault;
import com.matrix.common.vo.basic.response.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.net.Authenticator;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;

/**
 * @author liuweizhong
 * @since 2025-09-12 00:00
 */
@Slf4j
@Service
public class HttpApiServiceImpl implements IHttpApiService {


    @Override
    public BaseResponse<String> get(String url) {
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("http://openjdk.java.net/"))
                .timeout(Duration.ofMinutes(1))
                .GET()
                .build();
        HttpClient client = HttpClient.newBuilder()
                .version(HttpClient.Version.HTTP_2)
                .followRedirects(HttpClient.Redirect.ALWAYS)
//                .proxy(ProxySelector.of(new InetSocketAddress("www-proxy.com", 8080)))
                .authenticator(Authenticator.getDefault())
                .build();
        CompletableFuture<HttpResponse<String>> httpResponseCompletableFuture = client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
        return BaseResponse.success(httpResponseCompletableFuture.join().body());
    }

    @Override
    public BaseResponse<String> post(String url, String json) {
        return BaseResponse.success(SysDefault.SUCCESS.getValue());
    }
}
